package com.jxtc.enterprise.tenant.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.aspectj.TokenRequired;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import com.jxtc.enterprise.tenant.dto.deliveryTaskTemplate.DeliveryTaskCreateDTO;
import com.jxtc.enterprise.tenant.dto.deliveryTaskTemplate.DeliveryTaskUpdateDTO;
import com.jxtc.enterprise.tenant.service.TenantDeliveryTaskManageService;
import com.jxtc.enterprise.tenant.vo.deliveryTask.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/jxtc/enterprise/tenant/deliveryTaskManage")
@RequiredArgsConstructor
@Slf4j
@TokenRequired(role = TokenRequired.ROLE_TENANT)
public class TenantDeliveryTaskManageController {

    private final TenantDeliveryTaskManageService tenantDeliveryTaskManageService;

    /**
     * 接口 2.8.1 分页查询配送任务列表
     *
     * @param keyword  模糊查询关键字，根据 "配送员名称"、"公司名称" 或 "店铺名称" 模糊查询
     * @param pageNo   当前展示第几页，默认从 1 开始
     * @param pageSize 每页展示数据数量，默认为 10
     * @return 按照配送任务优先级和创建时间降序返回分页后的配送任务数据
     */
    @GetMapping("/queryDeliveryTask")
    public Result<PaginationResponseVO<DeliveryTaskInfoVO>> queryDeliveryTask(@RequestParam(required = false) String keyword,
                                                                              @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/queryDeliveryTask, keyword: {}, pageNo: {}, pageSize: {}, tenantId: {}", keyword, pageNo, pageSize, tenantId);

        PaginationResponseVO<DeliveryTaskInfoVO> vo = tenantDeliveryTaskManageService.pageDeliveryTaskByKeyword(keyword, tenantId, pageNo, pageSize);

        log.info("/queryDeliveryTask, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 接口 2.8.2 根据 ID 查询配送任务详情
     *
     * @param taskTemplateId 配送任务模板 ID
     * @return 配送任务信息
     */
    @GetMapping("/getDeliveryTaskById")
    public Result<DeliveryTaskDetailVO> getDeliveryTaskById(@RequestParam String taskTemplateId) {
        log.info("/getDeliveryTaskById, taskTemplateId: {}", taskTemplateId);
        DeliveryTaskDetailVO vo = tenantDeliveryTaskManageService.getDeliveryTaskById(taskTemplateId);
        log.info("/getDeliveryTaskById, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 接口 2.8.3 新增配送任务
     *
     * @param dto 配送任务创建信息
     * @return 新增配送任务成功时 code 字段为 200，data 字段为空；新增失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @PostMapping("/addDeliveryTask")
    public Result<Void> addDeliveryTask(@Valid @RequestBody DeliveryTaskCreateDTO dto, HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/addDeliveryTask, tenantId: {}, request: {}", tenantId, JSONObject.toJSONString(dto));
        tenantDeliveryTaskManageService.addDeliveryTask(tenantId, dto);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 接口 2.8.4 编辑配送任务
     *
     * @param dto 配送任务编辑信息
     * @return 编辑配送任务成功时 code 字段为 200，data 字段为空；编辑失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @PostMapping("/editDeliveryTask")
    public Result<Void> editDeliveryTask(@Valid @RequestBody DeliveryTaskUpdateDTO dto) {
        log.info("/editDeliveryTask, request: {}", JSONObject.toJSONString(dto));
        tenantDeliveryTaskManageService.editDeliveryTask(dto);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 接口 2.8.5 移除配送任务与取餐店铺的关系（已弃用，该操作直接放在接口 2.8.4 中实现）
     *
     * @param taskTemplateId 配送任务 ID
     * @param storeId        取餐店铺 ID
     * @return 移除成功时 code 字段为 200，data 字段为空；移除失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @Deprecated
    @DeleteMapping("/removeDeliveryTaskStoreRelation")
    public Result<Void> removeDeliveryTaskStoreRelation(@RequestParam String taskTemplateId, @RequestParam String storeId) {
        log.info("/removeDeliveryTaskStoreRelation, taskTemplateId: {}, storeId: {}", taskTemplateId, storeId);
        tenantDeliveryTaskManageService.removeDeliveryTaskStoreRelation(taskTemplateId, storeId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 接口 2.8.6 添加配送任务与取餐店铺的关系（已弃用，该操作直接放在接口 2.8.4 中实现）
     *
     * @param taskTemplateId 配送任务模板 ID
     * @param storeId        取餐店铺 ID
     * @return 添加成功时 code 字段为 200，data 字段为空；添加失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @Deprecated
    @GetMapping("/addDeliveryTaskStoreRelation")
    public Result<Void> addDeliveryTaskStoreRelation(@RequestParam String taskTemplateId, @RequestParam String storeId) {
        log.info("/addDeliveryTaskStoreRelation, taskTemplateId: {}, storeId: {}", taskTemplateId, storeId);
        tenantDeliveryTaskManageService.addDeliveryTaskStoreRelation(taskTemplateId, storeId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 接口 2.8.7 根据 ID 删除配送任务
     *
     * @param taskTemplateId 配送任务模板 ID
     * @return 删除成功时 code 字段为 200，data 字段为空；删除失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @DeleteMapping("/deleteDeliveryTaskById")
    public Result<Void> deleteDeliveryTaskById(@RequestParam String taskTemplateId) {
        log.info("/deleteDeliveryTaskById, taskTemplateId: {}", taskTemplateId);
        tenantDeliveryTaskManageService.deleteDeliveryTaskById(taskTemplateId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 接口 2.8.8 获取与租户合作的公司列表
     *
     * @param tenantId 租户 ID
     * @return code 字段为 200，data 字段为合作公司列表
     */
    @GetMapping("/queryCooperatingCompanyList")
    public Result<List<CooperatingCompanyVO>> queryCooperatingCompanyList(@RequestParam String tenantId) {
        log.info("/queryCooperatingCompanyList, tenantId: {}", tenantId);
        List<CooperatingCompanyVO> voList = tenantDeliveryTaskManageService.queryCooperatingCompanyList(tenantId);
        log.info("/queryCooperatingCompanyList, response: {}", JSONObject.toJSONString(voList));
        return ResultGenerator.genSuccessResult(voList);
    }

    /**
     * 接口 2.8.9 获取用餐时段、配送员和取餐店铺级联信息
     *
     * @param companyId 公司 ID
     * @return code 字段为 200，data 字段为用餐时段、配送员和取餐店铺级联信息
     */
    @GetMapping("/queryRiderMealTypeCascade")
    public Result<RiderMealTypeCascadeVO> queryRiderMealTypeCascade(@RequestParam String tenantId, @RequestParam String companyId) {
        log.info("/queryRiderMealTypeCascade, tenantId: {}, companyId: {}", tenantId, companyId);
        RiderMealTypeCascadeVO vo = tenantDeliveryTaskManageService.queryRiderStoreCascade(tenantId, companyId);
        log.info("/queryRiderMealTypeCascade, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    /**
     * 接口 2.8.10 获取店铺列表级联信息
     *
     * @param companyId 公司 ID
     * @param mealType  用餐时段
     * @return code 字段为 200，data 字段为店铺 ID 及店铺名称键值对列表
     */
    @GetMapping("/queryStoreListCascade")
    public Result<List<StoreIdWithNameVO>> queryStoreListCascade(@RequestParam String companyId, @RequestParam String mealType) {
        log.info("/queryStoreListCascade, companyId: {}, mealType: {}", companyId, mealType);
        List<StoreIdWithNameVO> voList = tenantDeliveryTaskManageService.queryStoreListCascade(companyId, mealType);
        log.info("/queryStoreListCascade, response: {}", JSONObject.toJSONString(voList));
        return ResultGenerator.genSuccessResult(voList);
    }
}
